//
//  ZDDStatusOriginalView.swift
//  小蜗牛_新浪微博
//
//  Created by 周冬冬 on 16/6/27.
//  Copyright © 2016年 我的mac. All rights reserved.
//

import UIKit
import SnapKit
class ZDDStatusOriginalView: UIView {
    //定义一个全局的原创微博的底部约束
    var originalViewbottomConstraint:Constraint?
    var status:ZDDStatuses?{
        didSet{
        //微博来源

        sourceLabel.attributedText = status?.sourceAttr
        //微博创建时间
        timeLabel.text = getWbTimeStr(status?.created_at ?? "")
        //用户昵称
        nameLabel.text = status?.user?.name
            if status?.user?.mbrank > 0 {
                nameLabel.textColor = ThemeColor
            }
        //认证图片
        vipImageView.image = status?.user?.verifiedImage
        //微博等级
        expiredImageView.image = status?.user?.mbrankImage
        //微博信息
        contentLabel.text = status?.text
        //用户头像
        headImageView.sd_setImageWithURL(NSURL(string: status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        //配图
            let count = status?.pic_urls?.count
            
            //卸载约束
            originalViewbottomConstraint?.uninstall()
            //判断
            if count > 0 {
            
                pictureView.hidden = false
                //赋值
              pictureView.pic_urls = status?.pic_urls
                //添加约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    originalViewbottomConstraint = make.bottom.equalTo(pictureView).offset(StautsMargin).constraint
                })
            
            }else {
            
                pictureView.hidden = true
                //添加约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    originalViewbottomConstraint = make.bottom.equalTo(contentLabel).offset(StautsMargin).constraint
                })
            
            }
            
        }
    
    }
    func getWbTimeStr(creatAt:String)->String{
    
        //时间格式
        let df = NSDateFormatter()
        //格式
        df.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        //时区
        df.locale = NSLocale(localeIdentifier: "en_US")
        //微博时间
        let wbDate1 = df.dateFromString(creatAt)!
        //判断是否是今年
        let isThisYear : Bool = getWbIsThisYear(wbDate1)
        if isThisYear {
        
            //今年

            let calendar = NSCalendar.currentCalendar()
            //如果是今天
            if calendar.isDateInToday(wbDate1) {
            //秒数计算
                let s = NSDate().timeIntervalSinceDate(wbDate1)
                //如果小于60秒
                if s < 60 {
                return "刚刚"
                
                }else if s >= 60 && s < 60 * 60 {
                //显示格式:xx分钟
                    return "\(Int(s/60))分钟"
                }else{
                return "\(Int(s/3600))小时前"
                }
            
            }else if calendar.isDateInYesterday(wbDate1){
            
            df.dateFormat = "昨天 HH:mm"
                
                return df.stringFromDate(wbDate1)
            
            
            }else{
            df.dateFormat = "MM月dd日 HH:mm"
                return df.stringFromDate(wbDate1)
            
            }
        
        
        }else{
        //不是今年
            df.dateFormat = "yyyy年MM月dd日 HH:mm"
        let result = df.stringFromDate(wbDate1)
            return result
        
        }
    
    }
    func getWbIsThisYear(wbDate:NSDate)->Bool {
        let df = NSDateFormatter()
        // 设置地区
        df.locale = NSLocale(localeIdentifier: "en_US")
        // 格式
        df.dateFormat = "yyyy"
    
        //设置微博时间
        let wbTimeString = df.stringFromDate(wbDate)
        //设置格式
        let currentWbTimeStr = df.stringFromDate(NSDate())
        
        if wbTimeString == currentWbTimeStr {
        
            //今年
            return true
        
        }else{
        return false
        }
    
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
    
         backgroundColor = UIColor.whiteColor()
        
        //添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(expiredImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //添加约束
        //头像
        headImageView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(StautsMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
//          昵称
        nameLabel.snp_makeConstraints{ (make) -> Void in
            make.leading.equalTo(headImageView.snp_trailing).offset(StautsMargin)
            make.top.equalTo(headImageView)
            }
        //等级
        expiredImageView.snp_makeConstraints{ (make) -> Void in
                make.leading.equalTo(nameLabel.snp_trailing).offset(StautsMargin)
                make.top.equalTo(nameLabel)
        }
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
                make.leading.equalTo(headImageView.snp_trailing).offset(StautsMargin)
            make.bottom.equalTo(headImageView.snp_bottom)
        
        }
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel.snp_top)
            make.leading.equalTo(timeLabel.snp_trailing).offset(StautsMargin)
        }
        //认证
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        //微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(StautsMargin)
            make.leading.equalTo(headImageView)
        }
        //配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(StautsMargin)
        }
        //原创微博
        self.snp_makeConstraints { (make) -> Void in
           originalViewbottomConstraint = make.bottom.equalTo(pictureView).offset(StautsMargin).constraint
        }
        
        
        timeLabel.text = "时间"
        sourceLabel.text = "来自磕坏一角的iPhone5S"
        
        headImageView.layer.cornerRadius = 17.5
        headImageView.layer.masksToBounds = true

    
    }

  //  MARK: - 懒加载控件
    
    
    //用户头像
    private lazy var headImageView:UIImageView = UIImageView(imageName: "avatar_default_big")
    //用户名字
    private lazy var nameLabel:UILabel = UILabel(textColor:UIColor.darkGrayColor(),textFont:15)
    //用户等级
    private lazy var expiredImageView:UIImageView = UIImageView(imageName: "common_icon_membership_expired")
    //微博时间
    private lazy var timeLabel:UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    //微博来源
    private lazy var sourceLabel:UILabel = UILabel(textColor: UIColor.grayColor(), textFont: 10)
    //认证
    private lazy var vipImageView:UIImageView = UIImageView(imageName: "avatar_vgirl")
    //微博内容
    private lazy var contentLabel :UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15, MaxWith: kScreenW - 2 * StautsMargin)
    //配图
    private lazy var pictureView:ZDDPictureInfo = ZDDPictureInfo()
}





















